//
//  BoardNameFormatter.m
//  PicUploader
//
//  Created by Chen Zhang on 9/13/08.
//  Copyright 2008 overboming. All rights reserved.
//

#import "AllBoardNameFormatter.h"

@implementation AllBoardNameFormatter


- (id)init
{
    self = [super init];
    if(self){
               
        NSDictionary *helper1 = [[NSDictionary dictionaryWithObjectsAndKeys:
								  @"56",@"Software",
								  @"416",@"American_Football",
								  @"94",@"Poetry",
								  @"103",@"Football_WD",
								  @"480",@"Olympics",
								  @"472",@"Real_Estate",
								  @"285",@"Appeal",
								  @"3",@"Notice",
								  @"429",@"ShaanXi",
								  @"134",@"Broadcast",
								  @"378",@"Chinese_04",
								  @"437",@"ShanXi",
								  @"430",@"HuNan",
								  @"259",@"City_Cafe",
								  @"266",@"Tobacco_Alcohol",
								  @"154",@"Music_Game",
								  @"76",@"Online_Game",
								  @"148",@"Military",
								  @"498",@"NingXia",
								  @"112",@"Feelings",
								  @"8",@"FDU_C.D.E.",
								  @"283",@"NeiMeng",
								  @"16",@"FDU_Economics",
								  @"106",@"Table_Tennis",
								  @"359",@"Mechanics_MEG",
								  @"183",@"Debate",
								  @"303",@"FDU_Nursing",
								  @"393",@"CS_Graduate",
								  @"319",@"Doctors_Nurses",
								  @"163",@"Romance",
								  @"258",@"Arch_Compiler",
								  @"442",@"HuBei",
								  @"499",@"Theatre",
								  @"97",@"FDU_SHMC",
								  @"256",@"Guoxue",
								  @"443",@"Marriage",
								  @"253",@"Lost_Found",
								  @"224",@"FDU_IMP",
								  @"293",@"ME",
								  @"93",@"Fine_Arts",
								  @"356",@"FDU_M.E.",
								  @"42",@"BBS_History",
								  @"273",@"Toy",
								  @"128",@"Cross_Music",
								  @"310",@"Doctor",
								  @"215",@"FenglinSecondhand",
								  @"81",@"Rock",
								  @"86",@"Book",
								  @"102",@"Health",
								  @"210",@"Animal",
								  @"162",@"Basic",
								  @"307",@"Piano",
								  @"306",@"Complaint",
								  @"160",@"M_League",
								  @"229",@"M_Library",
								  @"295",@"Visa",
								  @"225",@"Baseball",
								  @"239",@"Russian",
								  @"89",@"XiquQuyi",
								  @"5",@"New_Board",
								  @"107",@"Volleyball",
								  @"226",@"Art_Study",
								  @"191",@"BBS_Dev",
								  @"243",@"Zone9",
								  @"174",@"Board_Game",
								  @"211",@"M_North_PM",
								  @"111",@"Family",
								  @"221",@"German",
								  @"181",@"Graduate_SA",
								  @"361",@"RTS_Game",
								  @"267",@"BBS_RevLog",
								  @"234",@"FM_Book_Acoustic",
								  @"439",@"BaShu",
								  @"33",@"FDU_S.I.R.P.A.",
								  @"286",@"FM_PC",
								  @"7",@"Announce",
								  @"298",@"FDU_Brain",
								  @"46",@"Notepad",
								  @"489",@"GuiZhou",
								  @"343",@"FreeSoftware",
								  @"70",@"Magpie_Bridge",
								  @"26",@"FDU_Philosophy",
								  @"29",@"Campus_Digest",
								  @"330",@"FDUROP",
								  @"23",@"FDU_Journalism",
								  @"119",@"Oversea",
								  @"184",@"Net_Resource",
								  @"219",@"Japanese",
								  @"202",@"M_Career",
								  @"187",@"Badminton",
								  @"268",@"Politics",
								  @"170",@"DigitalLife",
								  @"426",@"JiangSu",
								  @"212",@"M_Ours_PM",
								  @"279",@"Pujiang",
								  @"485",@"C_FDpic",
								  @"32",@"FDU_M.S.",
								  @"22",@"FDU_Base",
								  @"264",@"Pie_Friend",
								  @"420",@"C.H.T._03",
								  @"50",@"EnglishTest",
								  @"104",@"KeepFit",
								  @"146",@"Philosophy",
								  @"282",@"FDU_TCFTCL",
								  @"362",@"FPS_Game",
								  @"6",@"SysOp",
								  @"260",@"JunQi",
								  @"167",@"FDU_O.S.E.",
								  @"20",@"M_Network",
								  @"37",@"FDU_Physics",
								  @"493",@"EXPO_2010",
								  @"45",@"MobilePhone",
								  @"261",@"HKMOTW_STU",
								  @"290",@"FM_Others",
								  @"427",@"FuJian",
								  @"59",@"Network",
								  @"434",@"BeiJing",
								  @"105",@"Sports",
								  @"484",@"C_Voting",
								  @"91",@"FairyTale",
								  @"127",@"Practice",
								  @"168",@"Astronomy",
								  @"71",@"Cartoon",
								  @"10",@"Commerce",
								  @"142",@"EE",
								  @"150",@"FDU_C.S.E.",
								  @"412",@"Architecture",
								  @"277",@"FM_Ecommodity",
								  @"62",@"WebDev",
								  @"166",@"Exam",
								  @"274",@"Money",
								  @"321",@"TVEntZ",
								  @"15",@"FDU_Management",
								  @"143",@"Linguistics",
								  @"318",@"LifeInNorth",
								  @"173",@"Buddhism",
								  @"431",@"Job_Plaza",
								  @"145",@"Nature",
								  @"223",@"Sports_Game",
								  @"446",@"JiangXi",
								  @"425",@"ShanDong",
								  @"51",@"Database",
								  @"409",@"Beauty_shopping",
								  @"136",@"History",
								  @"185",@"Psy_Hotline",
								  @"265",@"Wanted",
								  @"117",@"Love",
								  @"101",@"Football_CN",
								  @"141",@"Foreign_Language",
								  @"1",@"BM_Home",
								  @"66",@"Unix",
								  @"126",@"Loving_Heart",
								  @"252",@"Sociology",
								  @"75",@"Movie",
								  @"172",@"Billiards",
								  @"65",@"ANSI",
								  @"200",@"M_GuangHua",
								  @"413",@"FDU_IHE",
								  @"421",@"DV",
								  @"73",@"Food",
								  @"171",@"Graphics",
								  @"13",@"M_Jiangwan",
								  @"121",@"Teacher",
								  @"131",@"KaoYan",
								  @"394",@"Software_00",
								  @"390",@"Software_02",
								  @"403",@"Software_05",
								  @"481",@"Software_06",
								  @"190",@"Num_Analysis",
								  @"12",@"M_West_PM",
								  @"123",@"Creation",
								  @"82",@"Pop",
								  @"231",@"JazzBlues",
								  @"120",@"Single",
								  @"380",@"E.E._04EE",
								  @"197",@"Renju",
								  @"288",@"FM_Ticket",
								  @"220",@"Lessons",
								  @"21",@"FDU_Illumination",
								  @"281",@"Help_Counsellor",
								  @"201",@"Flash",
								  @"115",@"Graduate",
								  @"9",@"Test",
								  @"255",@"Cycling",
								  @"411",@"StoneStory",
								  @"395",@"Job_Big4",
								  @"78",@"Photography",
								  @"317",@"M_Zhangjiang",
								  @"34",@"FDU_Life",
								  @"152",@"Life_Science",
								  @"218",@"French",
								  @"241",@"Zone3",
								  @"242",@"Zone2",
								  @"244",@"Zone1",
								  @"248",@"Zone7",
								  @"247",@"Zone6",
								  @"246",@"Zone5",
								  @"245",@"Zone4",
								  @"52",@"Translation",
								  @"249",@"Zone8",
								  @"18",@"FDU_C.S.",
								  @"25",@"FDU_Chinese",
								  @"276",@"ZoneB",
								  @"155",@"ZoneA",
								  @"423",@"FDU_I.B.S.",
								  @"392",@"World_Students",
								  @"38",@"FDU_C.H.T.",
								  @"19",@"FDU_E.E.",
								  @"198",@"Swimming",
								  @"79",@"Riddle",
								  @"232",@"FDU_PublicHealth",
								  @"208",@"Traffic_Info",
								  @"43",@"Tea",
								  @"74",@"Ghost_Story",
								  @"35",@"FDU_Mathematics",
								  @"374",@"HaiNan",
								  @"153",@"Christianity",
								  @"441",@"GuangDong",
								  @"373",@"C.H.T._04",
								  @"435",@"DongBei",
								  @"217",@"Auto",
								  @"164",@"Folklore",
								  @"240",@"DotNet",
								  @"61",@"C",
								  @"269",@"FM_Commodity",
								  @"63",@"Programming",
								  @"500",@"FDU_ICES",
								  @"400",@"E.E._02EE",
								  @"88",@"Detective",
								  @"96",@"Whim",
								  @"161",@"Dance",
								  @"122",@"Undergraduate",
								  @"399",@"Mechanics_03",
								  @"80",@"Travel",
								  @"250",@"FDU_Pharmacy",
								  @"216",@"HiFi",
								  @"417",@"GalGame",
								  @"92",@"Literature",
								  @"233",@"Beauty",
								  @"90",@"Emprise",
								  @"85",@"Guitar",
								  @"222",@"ID_Story",
								  @"99",@"Card",
								  @"397",@"O.S.E._04",
								  @"189",@"PC_Market",
								  @"451",@"Job_Servant",
								  @"292",@"MathTools",
								  @"83",@"Classic",
								  @"415",@"Economics_SEG",
								  @"433",@"HeNan",
								  @"39",@"FDU_Macromol.S.",
								  @"436",@"GanSu",
								  @"98",@"Basketball",
								  @"186",@"Tennis",
								  @"100",@"Chess",
								  @"129",@"Stock",
								  @"254",@"Sports_Lottery",
								  @"418",@"M.E._04ME",
								  @"47",@"Embedded",
								  @"203",@"JCS",
								  @"235",@"IQ",
								  @"459",@"M.E._05ME",
								  @"404",@"FlightSimulate",
								  @"449",@"Job_IT",
								  @"84",@"Chinese_Music",
								  @"147",@"Physics",
								  @"95",@"Fantasy",
								  @"209",@"FDU_Development",
								  @"114",@"Girl",
								  @"193",@"Astrology",
								  @"180",@"Flower",
								  @"444",@"FDU_CHGS",
								  @"238",@"Service",
								  @"64",@"HackerVsSecurity",
								  @"383",@"E.E._05EE",
								  @"53",@"M_GSU",
								  @"28",@"FDU_S.S.D.P.P.",
								  @"108",@"WuShu",
								  @"159",@"NR_Music",
								  @"275",@"Advice",
								  @"11",@"PIC",
								  @"27",@"FDU_F.L.",
								  @"377",@"Running",
								  @"130",@"News",
								  @"158",@"NR_TV",
								  @"55",@"Multimedia",
								  @"341",@"E.E._01EE",
								  @"67",@"Joke",
								  @"388",@"S.I.R.P.A._03",
								  @"389",@"S.I.R.P.A._04",
								  @"227",@"FDU_SU_Club",
								  @"188",@"Fashion",
								  @"194",@"BBS_Help",
								  @"205",@"FDU_Software",
								  @"199",@"OA",
								  @"24",@"FDU_Mechanics",
								  @"196",@"FDU_News",
								  @"31",@"FDU_Chemistry",
								  @"60",@"Java",
								  @"445",@"GuangXi",
								  @"214",@"Entertainment",
								  @"36",@"FDU_E.S.E.",
								  @"228",@"Weiqi",
								  @"432",@"ZheJiang",
								  @"175",@"TaiWan",
								  @"113",@"Friend",
								  @"302",@"FDU_LAM",
								  @"124",@"GoAbroad",
								  @"44",@"AI",
								  @"57",@"Virus",
								  @"87",@"Classical_Poem",
								  @"271",@"Celebration",
								  @"109",@"Boy",
								  @"453",@"HeBei",
								  @"204",@"Algorithm",
								  @"140",@"Economics",
								  @"176",@"Outdoors",
								  @"460",@"Chemistry_05",
								  @"376",@"Chemistry_04",
								  @"177",@"Chemistry",
								  @"382",@"Chemistry_01",
								  @"457",@"GuangHuaBei",
								  @"410",@"Chemistry_03",
								  @"375",@"Chemistry_02",
								  @"476",@"XinJiang",
								  @"137",@"World_Affairs",
								  @"157",@"NR_Cartoon",
								  @"156",@"NR_Movie",
								  @"287",@"MerchantAgent",
								  @"41",@"Psychology",
								  @"133",@"M_Management",
								  @"291",@"OMTV",
								  @"305",@"GoneWithTheWind",
								  @"116",@"Card_World",
								  @"458",@"Railway",
								  @"195",@"Finance",
								  @"72",@"Collection",
								  @"68",@"Video_Game",
								  @"237",@"Calligraphy",
								  @"358",@"FDU_A.D.",
								  @"17",@"FDU_Law",
								  @"419",@"Physics_98",
								  @"438",@"YunNan",
								  @"262",@"TeX",
								  @"440",@"AnHui",
								  @"58",@"MS_Windows",
								  @"77",@"TianJin",
								  @"213",@"House",
								  @"406",@"Physics_04",
								  @"381",@"Physics_05",
								  @"414",@"Physics_00",
								  @"408",@"Physics_01",
								  @"386",@"Physics_02",
								  @"387",@"Physics_03",
								  @"294",@"Wind_Instrument",
								  @"118",@"Memory",
								  @"144",@"Mathematics",
								  @"483",@"EuroCup",
								  @"289",@"FM_Beauty_Dress",
								  @"182",@"TV",
								  @"401",@"Mac",
								  @"492",@"Scripts",
								  @"138",@"Law",
								  @"132",@"Campus_Press",
								  @"69",@"PC_Game",
								  @"257",@"Insurance_Act",
								  @"110",@"Employees",
								  @"422",@"Moonlight",
								  @"270",@"FDU_D.S.S.",
								  @"263",@"ClassicLiterature",
								  @"40",@"Job_Intern",
								  @"251",@"Speed",
								  @"54",@"Hardware",
								  @"165",@"Skate",
								  @"179",@"Geography",nil] retain];
        
        
        
        boardNameDic = [[NSDictionary alloc] initWithDictionary:helper1];    
        
    }
    
    return self;
}

- (NSString *)stringForObjectValue:(id)obj
{
    if (![obj isKindOfClass:[NSString class]]) {
        return nil;
    }
    else{
        return [NSString stringWithFormat:@"%@",obj];
    }
}

- (void)dealloc
{
    [boardNameDic release];
    [super dealloc];
}


- (BOOL)getObjectValue:(id *)obj
             forString:(NSString *)string
      errorDescription:(NSString **)errorString
{
    // Look up the color for 'string'
    NSString *matchingKey = [self firstBoareNameForPartialString:string];
    if (matchingKey) {
//        *obj = [boardNameDic objectForKey:matchingKey];
		*obj = matchingKey;
        return YES;
    } else {
        // Occasionally, 'errorString' is NULL
        if (errorString != NULL) {
            *errorString = [NSString stringWithFormat:
                            @"'%@' is not a valid board name", string];
        }
        return NO;
    }
}

- (BOOL)isPartialStringValid:(NSString *)partial
            newEditingString:(NSString **)newString
            errorDescription:(NSString **)error
{
    if ([partial length] == 0){
        return YES;
    }
    NSString *match = [self firstBoareNameForPartialString:partial];
    if (match) {
        return YES;
    } else {
        if (error) {
            *error = @"No such board name";
        }
        return NO;
    }
    
}


- (BOOL)isPartialStringValid:(NSString **)partial
       proposedSelectedRange:(NSRange *)selPtr
              originalString:(NSString *)origString
       originalSelectedRange:(NSRange)origSel
            errorDescription:(NSString **)error
{
    // Zero-length strings are fine
    if ([*partial length] == 0) {
        return YES;
    }
    NSString *match = [self firstBoareNameForPartialString:*partial];
    // No color match?
    if (!match) {
        return NO;
    }
    
    // If this would not move the beginning of the selection, it
    // is a delete
    if (origSel.location == selPtr->location) {
        return YES;
    }
    
    // If the partial string is shorter than the
    // match, provide the match and set the selection
    if ([match length] != [*partial length]) {
        selPtr->location = [*partial length];
        selPtr->length = [match length] - selPtr->location;
        *partial = match;
        return NO;
    }
    return YES;
}

- (NSString *)firstBoareNameForPartialString:(NSString *)string
{
    // Is the key zero-length?
    if ([string length] == 0) {
        return nil;
    }
    for (NSString *key in [boardNameDic allKeys]) {
        NSRange whereFound = [key rangeOfString:string
                                        options:NSCaseInsensitiveSearch];
        // Does the string match the beginning of the board name?
        if ((whereFound.location == 0) && (whereFound.length > 0)) {
            return key;
        }
    }
    // If no match is found, return nil
    return nil;
}

@synthesize boardNameDic;
@end
